package com.cjjd.c.feature.subscribe.write_order;

import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.base.MyBaseAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ----------------------------------
 * Created by xyd on 2017/12/19.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class BookOrderAdapter extends MyBaseAdapter {

    public BookOrderAdapter(FragmentActivity mContext) {
        super(mContext);
    }

    public SparseArray<String> nameArray = new SparseArray();

    public SparseArray<String> getNameArray(){
        return nameArray;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_book_order, parent, false);
            viewHolder = new ViewHolder(convertView);
            viewHolder.tvBookOrderName.setTag(position);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tvBookOrderId.setText("入住人" + (position + 1));

        viewHolder.tvBookOrderName.addTextChangedListener(new MyTextWatcher(viewHolder));
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.tv_book_order_id)
        TextView tvBookOrderId;
        @Bind(R.id.tv_book_order_name)
        EditText tvBookOrderName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    class MyTextWatcher implements TextWatcher {
        public MyTextWatcher(ViewHolder holder) {
            mHolder = holder;
        }

        private ViewHolder mHolder;

        @Override
        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            int position = (Integer) mHolder.tvBookOrderName.getTag();
            if (s != null){
                nameArray.put(position, s.toString());
            }else {
                nameArray.put(position, "");
            }
        }
    }
}
